package org.jeecg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.domain.dto.HistoryOrdersSyncDto;
import org.jeecg.domain.po.HistoryOrdersSync;
import org.jeecg.service.HistoryOrdersSyncService;
import org.jeecg.mapper.HistoryOrdersSyncMapper;
import org.jeecg.utils.ResponseResult;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* @author 32327
* @description 针对表【history_orders_sync(历史订单完成15天后同步到历史订单同步表中，通过canal同步到历史订单库中；1天后删除（删除条件当天数据和历史订单库中的订单数据数量一致）)】的数据库操作Service实现
* @createDate 2025-01-02 14:05:44
*/
@Service
public class HistoryOrdersSyncServiceImpl extends ServiceImpl<HistoryOrdersSyncMapper, HistoryOrdersSync>
    implements HistoryOrdersSyncService{

    @Override
    public ResponseResult findPage(HistoryOrdersSyncDto dto) {
        //        当前凌晨时间00:00
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date midnightDate = calendar.getTime();
        System.out.println(midnightDate);
        dto.setMorningTime(midnightDate);
        // 获取当前日期的凌晨0:00对应的LocalDateTime
        LocalDateTime currentMidnight = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        // 减去365天得到365天前的凌晨0:00对应的LocalDateTime
        LocalDateTime threeHundredSixtyFiveDaysAgoMidnight = currentMidnight.minus(365, ChronoUnit.DAYS);
        // 将LocalDateTime转换为Date类型
        Date date = Date.from(threeHundredSixtyFiveDaysAgoMidnight.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(date);
        dto.setYearTime(date);
        IPage page = new Page(dto.getPageNum(),dto.getPageSize());
        LambdaQueryWrapper<HistoryOrdersSync> eq = Wrappers.<HistoryOrdersSync>lambdaQuery()
                .eq(HistoryOrdersSync::getServeProviderType, 3)
                .between(HistoryOrdersSync::getSortTime, dto.getYearTime(), dto.getMorningTime())
                .like(dto.getId() != null, HistoryOrdersSync::getId, dto.getId())
                .eq(dto.getOrdersStatus() != null, HistoryOrdersSync::getOrdersStatus, dto.getOrdersStatus());
        this.page(page, eq);
//        List<HistoryOrdersSync> list = this.list(eq);
//        page.setRecords(list);
        return ResponseResult.okResult(page);
    }

    @Override
    public ResponseResult findHistoryById(String id) {
        HistoryOrdersSync byId = this.getById(id);
        return ResponseResult.okResult(byId);
    }
}




